<?php
header("Content-Type: text/html; charset=iso-8859-1");
class bref{
	var $fields;

	function bref($id){
		$field = (func_num_args() > 1) ? func_get_arg(1) : "id";
		$q = "desc brefs";
		$r = ExecRequete($q);
		while($s = LigneSuivante($r)){
			 $fields[$s->Field] = $s->Type;
		}
		$q = "select * from brefs where $field = '$id'";
		$r = ExecRequete($q);
		if(!mysql_num_rows($r)){
			foreach($fields as $field) $this->$field = "";
		}
		else{
			$s = LigneSuivante($r);
			foreach($fields as $field => $type){
				$this->$field = $s->$field;
			}
		}

	}

	function fields(){
		$q = "desc brefs";
		$r = ExecRequete($q);
		while($s = LigneSuivante($r)){
			 $fields[$s->Field] = $s->Type;
		}
		return $fields;
	}


	function delete(){
		$q = "delete from brefs where id = '$this->id' limit 1";
		if(ExecRequete($q)) return 1;
		else return 0;
	}

	function insert(){
		if($this->date_creation && $this->date_expiration && $this->contenu){
			$texte = ($this->valide) ? "br&egrave;ve publi&eacute;e" : "br&egrave;ve mise en attente de publication";
			$q = "insert into brefs (contenu,date_creation, date_expiration,valide) values ('".(addslashes($this->contenu))."','".$this->date_creation."','".$this->date_expiration."','".$this->valide."')";
			if(ExecRequete($q))	return $texte;
			else return "D&eacute;sol&eacute;, une erreur s'est produite !!";
		}
		else return "Soit le contenu, soit le titre est manquant";
	}
	function update(){
		$fields = $this->fields();
		if($this->id){
			$q = "update brefs set ";
			foreach($fields as $field => $type){
				if($this->$field){
					if($this->$field == 'now()') $q .= $field." = ".$this->$field.", ";
					else $q .= $field." = '".(addslashes($this->$field))."', ";
				}
			}
			if(strstr($q,",") !== false){
				$q = substr($q,0,-2);
				$q .= " where id = '$this->id'";
				if(ExecRequete($q)) return 1;
			}
			else return 0;
		}
		else return 0;
	}
}
?>
